package a51qindian.com.comprehensive.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import a51qindian.com.comprehensive.Const;
import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.adapter.TaskAcceptedAdapter;
import a51qindian.com.comprehensive.adapter.TaskCompletedAdapter;
import a51qindian.com.comprehensive.adapter.TaskUnAcceptedAdapter;
import a51qindian.com.comprehensive.base.QDBaseFragment;
import a51qindian.com.comprehensive.items.LinearItemDecoration;
import butterknife.BindView;

/**
 * Author: Li
 * Date: 2017/9/13 0013
 * Description: 任务处理
 */

public class TaskFragment extends QDBaseFragment {
    @BindView(R.id.recycler_task)
    RecyclerView mRecyclerTask;

    private String mType;
    private TaskUnAcceptedAdapter mUnAcceptedAdapter;
    private TaskAcceptedAdapter mAcceptedAdapter;
    private TaskCompletedAdapter mCompletedAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task;
    }

    public static TaskFragment newInstance(String taskType) {

        Bundle bundle = new Bundle();
        bundle.putString(Const.TASK_TYPE, taskType);
        TaskFragment fragment = new TaskFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mType=getArguments().getString(Const.TASK_TYPE,"");
        mRecyclerTask.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerTask.addItemDecoration(new LinearItemDecoration());
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        showTask(mType);
    }

    @Override
    protected void onFirstVisible() {
        super.onFirstVisible();
        showTask(mType);
    }

    /**
     * 显示任务
     * @param type  任务类别
     */
    private void showTask(String type){
        switch (type) {
            case Const.TASK_UN_ACCEPTED:
                mUnAcceptedAdapter=new TaskUnAcceptedAdapter(mContext);
                mRecyclerTask.setAdapter(mUnAcceptedAdapter);
                mUnAcceptedAdapter.setUnAcceptedOpListener(new TaskUnAcceptedAdapter.OnUnAcceptedOpListener() {
                    @Override
                    public void onCall(final String phone) {
                        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
                        builder.setTitle("拨打电话：")
                                .setMessage(phone)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent=new Intent(Intent.ACTION_DIAL);
                                        intent.setData(Uri.parse("tel:"+phone));
                                        startActivity(intent);
                                    }
                                })
                                .setNegativeButton("取消", null)
                                .create()
                                .show();
                    }

                    @Override
                    public void onAccept() {
                        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
                        builder.setTitle("接受任务")
                                .setPositiveButton("确定", null)
                                .setNegativeButton("取消", null)
                                .create()
                                .show();
                    }
                });
                break;
            case Const.TASK_ACCEPTED:
                mAcceptedAdapter=new TaskAcceptedAdapter(mContext);
                mRecyclerTask.setAdapter(mAcceptedAdapter);
                mAcceptedAdapter.setAcceptedOpListener(new TaskAcceptedAdapter.OnAcceptedOpListener() {
                    @Override
                    public void onCall(final String phone) {
                        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
                        builder.setTitle("拨打电话：")
                                .setMessage(phone)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent=new Intent(Intent.ACTION_DIAL);
                                        intent.setData(Uri.parse("tel:"+phone));
                                        startActivity(intent);
                                    }
                                })
                                .setNegativeButton("取消", null)
                                .create()
                                .show();
                    }

                    @Override
                    public void onGoToFinish() {
                        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
                        builder.setTitle("前去完成任务")
                                .setPositiveButton("确定", null)
                                .setNegativeButton("取消", null)
                                .create()
                                .show();
                    }
                });
                break;
            case Const.TASK_COMPLETED:
                mCompletedAdapter=new TaskCompletedAdapter(mContext);
                mRecyclerTask.setAdapter(mCompletedAdapter);
                mCompletedAdapter.setCompletedOpListener(new TaskCompletedAdapter.OnCompletedOpListener() {
                    @Override
                    public void onCall(final String phone) {
                        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
                        builder.setTitle("拨打电话：")
                                .setMessage(phone)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent=new Intent(Intent.ACTION_DIAL);
                                        intent.setData(Uri.parse("tel:"+phone));
                                        startActivity(intent);
                                    }
                                })
                                .setNegativeButton("取消", null)
                                .create()
                                .show();
                    }
                });
                break;
        }
    }
}
